package sy6_Behavioral_Pattern.mediator.mydo;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

abstract class Mediator{
    public abstract void dispatcherMessage(String msg,Colleadgue sender);
}

abstract class Colleadgue{
    protected Mediator mediator;
    protected String username;

    public Colleadgue(Mediator mediator) {
        this.mediator = mediator;
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    public void sendMessage(String msg){
        mediator.dispatcherMessage(msg,this);
    }
    public abstract void recieveMessage(String msg);
}
class ConcreteMediator extends Mediator{
    protected ConcreteColleague from;
    protected ConcreteColleague to;
    List<ConcreteColleague> concreteColleagues=new ArrayList<>();

    public void setFrom(ConcreteColleague from) {
        this.from = from;
    }

    public void setTo(ConcreteColleague to) {
        this.to = to;
    }
    @Override
    public void dispatcherMessage(String msg, Colleadgue who) {
//        System.out.println(who.getUsername()+"发了一条信息，内容是【"+msg+"】");
        for (ConcreteColleague colleague : concreteColleagues) {
            if(colleague!= who){
                if (who == from){
                    to.recieveMessage(from.getUsername()+"发送给"+to.getUsername()+"的消息是【"+msg+"】");
                }else {
                    from.recieveMessage(to.getUsername()+"发送给"+from.getUsername()+"的消息是【"+msg+"】");
                }
//                System.out.println(colleague.getUsername()+"收到来自"+who.getUsername()+"的消息");
//                ((Colleadgue)colleague).recieveMessage(msg);
            }
        }
    }

    public void registerColleague(Colleadgue colleadgue) {
        concreteColleagues.add((ConcreteColleague) colleadgue);
    }

    public void getConcreteColleagues() {
        for (ConcreteColleague concreteColleague : concreteColleagues) {
            System.out.println(concreteColleague);
        }
    }
}

class ConcreteColleague extends Colleadgue{

    public ConcreteColleague(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void recieveMessage(String msg) {
        System.out.println(msg);
    }
}
public class Client {
    public static void main(String[] args) {
        ConcreteMediator mediator = new ConcreteMediator();
        ConcreteColleague colleague1 = new ConcreteColleague(mediator);
        colleague1.setUsername("张三");
        ConcreteColleague colleague2 = new ConcreteColleague(mediator);
        colleague2.setUsername("李四");
        ConcreteColleague colleague3 = new ConcreteColleague(mediator);
        colleague3.setUsername("王五");
        //设置消息的发送者及接收者

        mediator.registerColleague(colleague1);
        mediator.registerColleague(colleague2);

        //张三为发送者，李四接收
        mediator.setFrom(colleague1);
        mediator.setTo(colleague2);

        colleague1.sendMessage("李四，你好");
        colleague2.sendMessage("张三，你好");
//        mediator.getConcreteColleagues();
    }
}
